$(function() {
	var money = sessionStorage.getItem('money');
	var orderId = sessionStorage.getItem('orderId');
	$('.payAllMoney').text(fmoney(money, 2));

	$('.toPay').click(function() {
		//支付方式
		var payType = $('input:radio[name="bank"]:checked').val();
		if(payType == null) {
			layer.alert("请选择支付方式");
			return;
		} else {
			pay(payType, orderId);
		}
	})

})
 
 var init;
function pay(paytype, orderId) {
	//微信
	if(paytype == 3) {
		//启动定时器
		 init = window.setInterval(function() {
			pay_status();
		}, 2000);
		$("#myQRCode").html("");
		$.ajax({
			url: urlcore + "/api/pay/pc/wxpay?orderId=" + orderId,
			type: "get",
			dataType: 'json',
			contentType: "application/json;charset=utf-8",
			success: function(data) {
				if(data.success == true) {
					var da = data.data;
					//二维码显示与取消
					$(function() {
						$("#myQRCode").fadeIn();

						$(".closeMymyQRCode").on('click', function() {
							$("#myQRCode").fadeOut();
							window.clearInterval(init); //销毁定时器
						})
					})
					var myQRCode =
						'<div align="center">' +
						'				<div style="width: 682px;height: 475px;border: 1px solid #ADADAD;padding: 30px;margin-top: 100px;background: #FDFAF9;">' +
						'					<h4 class="closeMymyQRCode" style="margin: -22px -639px 0 0;cursor: pointer;">X</h4>' +
						'					<div style="margin: 50px 410px 0px 6px;"><img src="images/sao.png"</div>' +
						'					' +
						'					<div style="margin: 0 3px 0 0;"><img src="' + da.imgUrl + '" style="width: 206px;height: 206px;"/></div>' +
						'					<div style="margin: -264px 0 0 291px;"><img src="images/weixin2.png"</div>' +
						'				</div>' +
						'			</div>';
					$("#myQRCode").html(myQRCode);
				}
			},
			error: function() {
				layer.alert("error");
			}
		});
	} else {

		//支付宝
		location.href = urlcore + "/api/pay/pc/alipay?orderId=" + orderId;

	}

}

function pay_status() {

	$.ajax({
		url: urlcore + "/api/pay/pc/getOrderStatus",
		type: "get",
		dataType: 'json',
		contentType: "application/json;charset=utf-8",
		success: function(data) {
			if(data.success == true) {
				var da = data.data;

				if(da.status != null && da.status == 1) {
					//跳转到结果页面，并传递状态
					window.location.href = '12.2.1zhifu_chenggong.html?payType=' + 3 + "&payMoney=" + da.payMoney + "&outTradeNo=" + da.outTradeNo;
					window.clearInterval(init); //销毁定时器
				}

			}
		}
	});
}